/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.jsf.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.model.SelectItem;
import ru.ssau.berlin.ejb.facade.EventFacadeLocal;
import ru.ssau.berlin.ejb.facade.GroupFacadeLocal;
import ru.ssau.berlin.ejb.facade.PersonFacadeLocal;
import ru.ssau.berlin.entity.Event;
import ru.ssau.berlin.entity.EventParameter;
import ru.ssau.berlin.entity.Person;
import ru.ssau.berlin.entity.PersonGroup;
import ru.ssau.berlin.entity.PersonParameter;
import ru.ssau.berlin.jsf.util.FacesApplication;

/**
 *
 * @author Caesar
 */
public class QueryController {

    @EJB
    private PersonFacadeLocal personFacade;
    @EJB
    private GroupFacadeLocal groupFacade;
    @EJB
    private EventFacadeLocal eventFacade;

    private FacesApplication application = FacesApplication.getInstance();

    private Map<PersonGroup, List<Person>> nonAchievers;
    private List<PersonGroup> faculties;

    private Map<Person, List<Event>> schedule;
    private List<Person> instructors;
    private Map<Event, String> dates;

    private Map<Person, String> salaries;
    private List<Person> employees;
    private List<SelectItem> items;
    private PersonGroup department;

    private PersonGroup group;
    private List<Event> events;

    private Person student;
    private String day;

    public Map<PersonGroup, List<Person>> getNonAchievers() {
        return nonAchievers;
    }

    public List<PersonGroup> getFaculties() {
        return faculties;
    }

    public List<Person> getInstructors() {
        return instructors;
    }

    public Map<Person, List<Event>> getSchedule() {
        return schedule;
    }

    public Map<Event, String> getDates() {
        return dates;
    }

    public Map<Person, String> getSalaries() {
        return salaries;
    }

    public List<SelectItem> getItems() {
        return items;
    }

    public PersonGroup getDepartment() {
        return department;
    }

    public void setDepartment(PersonGroup department) {
        this.department = department;
    }

    public List<Person> getEmployees() {
        return employees;
    }

    public PersonGroup getGroup() {
        return group;
    }

    public void setGroup(PersonGroup group) {
        this.group = group;
    }

    public List<Event> getEvents() {
        return events;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public Person getStudent() {
        return student;
    }

    public void setStudent(Person student) {
        this.student = student;
    }

    public String showNonAchievers() {
        nonAchievers = new HashMap<PersonGroup, List<Person>>();
        faculties = groupFacade.findFaculties();
        for (PersonGroup iter: faculties) {
            nonAchievers.put(iter, personFacade.findNonAchievers(iter));
        }
        return application.getNavigator().goToNonAchievers();
    }

    public String showInstructorsSchedule() {
        schedule = new HashMap<Person, List<Event>>();
        instructors = personFacade.findInstructors();
        for (Person person: instructors) {
            events = eventFacade.findInstructorSchedule(person);
            schedule.put(person, events);
            fillDates(events);
        }
        return application.getNavigator().goToInstructorsSchedule();
    }

    public String showSalaries() {
        items = new ArrayList<SelectItem>();
        for (PersonGroup iter: groupFacade.findDepartments()) {
            items.add(new SelectItem(iter, iter.getName()));
        }
        salaries = new HashMap<Person, String>();
        if (department != null) {
            employees = personFacade.findSalaries(department);
            for (Person employee: employees) {
                for (PersonParameter param: employee.getParameters()) {
                    String name = param.getAttribute().getName();
                    if (param.getAttribute().getType().getName().equalsIgnoreCase("float") &&
                            (name.equalsIgnoreCase("salary") || name.equalsIgnoreCase("Зарплата"))) {
                        salaries.put(employee, param.getParameterValue());
                        break;
                    }
                }
            }
        } else {
            employees = new ArrayList<Person>(2);
        }
        return application.getNavigator().goToSalaries();
    }

    public String showGroupSchedule() {
        items = new ArrayList<SelectItem>();
        for (PersonGroup iter: groupFacade.findStudentGroups()) {
            items.add(new SelectItem(iter, iter.getName()));
        }
        if (group != null) {
            events = eventFacade.findGroupSchedule(group);
            fillDates(events);
        } else {
            events = new ArrayList<Event>(2);
        }
        return application.getNavigator().goToGroupSchedule();
    }

    public String showStudentSchedule() {
        items = new ArrayList<SelectItem>();
        for (Person iter: personFacade.findStudents()) {
            items.add(new SelectItem(iter, iter.getName()));
        }
        if (student != null && day != null && !day.equals("")) {
            events = eventFacade.findStudentSchedule(student, day);
            fillDates(events);
        } else {
            events = new ArrayList<Event>(2);
        }
        return application.getNavigator().goToStudentSchedule();
    }

    private void fillDates(List<Event> events) {
        dates = new HashMap<Event, String>();
        for (Event event: events) {
            for (EventParameter param: event.getParameters()) {
                String name = param.getAttribute().getName();
                if (param.getAttribute().getType().getName().equalsIgnoreCase("datetime") &&
                        (name.equalsIgnoreCase("date") || name.equalsIgnoreCase("Дата"))) {
                    dates.put(event, param.getParameterValue());
                    break;
                }
            }
        }
    }

}
